//
//  YKClientDataManageController.m
//  YunKang
//
//  Created by leoking870 on 2017/9/25.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKClientDataManageController.h"
#import "YKClientDataItem.h"
#import "YKClientListHeaderView.h"
#import "YKClientDataDetailController.h"
#import "YKClientDataManageHeaderView.h"
#import "YKClientDataDetailV2Controller.h"

@interface YKClientDataManageController ()

@property(nonatomic, strong) YKClientDataManageHeaderView *headerView;
@property (nonatomic, copy) NSString *customertype;
@end

@implementation YKClientDataManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"客户资料管理";
    
//    YKClientListHeaderView* headerView = [[YKClientListHeaderView alloc]initWithTitles:@[@"客户分类",@"客户名称",@"维护时间"] weights:@[@2,@5,@2]];
//    headerView.textFont = [UIFont systemFontOfSize:16];
//    headerView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];
//    [self.view addSubview:headerView];
//
//    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.top.trailing.equalTo(self.view);
//    }];

    self.headerView = [YKClientDataManageHeaderView viewFromNibName:@"YKClientDataManageHeaderView"];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.top.equalTo(self.headerView.mas_bottom);
    }];

    self.dataSource.totalPageKey = @"page.totalpage";
    self.dataSource.currentPageKey = @"curpage";
    
    [self startLoadData];
}

- (NSDictionary *)pageViewControllerParameters {
    return @{
             kTFPageViewControllerRequestParameter:@{@"c_name":[self searchText]?:@"",@"customertype":self.customertype?:@""},
             kTFPageViewControllerItemClass:[YKClientDataItem class],
             kTFPageViewControllerModelClass:[YKClientDataModel class],
             kTFPageViewControllerRequestURL:@"customerDataManager/customerDataManagerList.htm",
             kTFPageViewControllerDataKeyPath:@"list"
             };
}

- (void)beginSearchWithText:(NSString *)text {
    [self startLoadData];
}


- (void)didFinishLoad:(TFDataLoadPolicy)loadPolicy object:(id)object sections:(NSArray<TFTableViewSection *> *)sections error:(NSError *)error {
    [super didFinishLoad:loadPolicy object:object sections:sections error:error];
    if (loadPolicy == TFDataLoadPolicyNone) {
        NSDictionary *dictionary = object[@"customerTypeMap"];
        self.headerView.data = dictionary;
        self.headerView.data2 = object[@"customerCountMap"];
        __weak typeof(self) wself = self;
        self.headerView.tapAction = ^(NSString *code){
            wself.customertype = code;
            [wself startLoadData];
        };
    }
}

- (void)customizeItem:(YKClientDataItem *)item {
    @weakify(self)
    item.selectionHandler = ^(__kindof TFTableViewItem *item, NSIndexPath *indexPath) {
        YKClientDataDetailV2Controller* vc = [[YKClientDataDetailV2Controller alloc]init];
        vc.mYKClientDataModel = item.model;
        [self_weak_.navigationController pushViewController:vc animated:YES];
    };
}
- (void)onAddItemTapped:(id)sender {
    
}

@end
